package com.boc.langxiang.ui.activity.common;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.boc.langxiang.R;
import com.boc.langxiang.contract.common.AddressManage2Contract;
import com.boc.langxiang.entity.events.AddAddressSuccessEvent;
import com.boc.langxiang.entity.events.CityReadDoneEvent;
import com.boc.langxiang.entity.request.AddAddressRequest;
import com.boc.langxiang.entity.request.EditAddressRequest;
import com.boc.langxiang.entity.response.AddressEntity;
import com.boc.langxiang.manager.CityManeger;
import com.boc.langxiang.presenter.common.AddressManage2Presenter;
import com.boc.langxiang.ui.activity.BaseToolBarActivity;
import com.boc.langxiang.util.KeyBoardUtil;
import com.boc.langxiang.util.StringUtil;
import com.boc.langxiang.util.T;
import com.boc.pickview.OptionsPickerView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 添加地址,修改地址
 */
public class AddAddressActivity extends BaseToolBarActivity implements AddressManage2Contract.View {

    @BindView(R.id.name)
    EditText mName;
    @BindView(R.id.phone)
    EditText mPhone;
    @BindView(R.id.ems)
    EditText mEms;
    @BindView(R.id.img01)
    ImageView mImg01;
    @BindView(R.id.city)
    TextView mCity;
    @BindView(R.id.detail)
    EditText mDetail;
    @BindView(R.id.save)
    TextView mSave;

    private OptionsPickerView optionsPickerView;

    private String provinceS;
    private String cityS;
    private String districtS;
    private AddressManage2Contract.Presenter mPresenter;
    private AddressEntity mAddress;
    private boolean mIsEdit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_address);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        Intent intent = getIntent();
        boolean edit = intent.getBooleanExtra("edit", false);
        if (edit) {
            setToolBarTitle("修改收货地址");
        } else {
            setToolBarTitle("新建收货地址");
        }

        mPresenter = new AddressManage2Presenter(this);

        mAddress = intent.getParcelableExtra("address");
        if (mAddress != null) {
            mIsEdit = true;
            matchAddress();
        }
    }

    private void matchAddress() {
        mName.setText(StringUtil.getValue(mAddress.getUsername()));
        mPhone.setText(StringUtil.getValue(mAddress.getPhone()));
        mEms.setText(StringUtil.getValue(mAddress.getZipcode()));
        provinceS = mAddress.getProvince();
        cityS = mAddress.getCity();
        districtS = mAddress.getDistrict();
        mCity.setText(StringUtil.getValue(mAddress.getProvince() + mAddress.getCity() + mAddress.getDistrict()));
        mDetail.setText(StringUtil.getValue(mAddress.getAddress()));
    }

    @Override
    protected boolean isSetTypeFace() {
        return true;
    }

    @OnClick({R.id.city, R.id.save})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.city:
                KeyBoardUtil.closeKeybord(this);
                CityManeger.getCity();
                break;
            case R.id.save:
                String name = mName.getText().toString();
                if (TextUtils.isEmpty(name)) {
                    T.showShort(mContext, "请输入收件人姓名");
                }
                String phone = mPhone.getText().toString();
                if (TextUtils.isEmpty(phone)) {
                    T.showShort(mContext, "请输入收件人手机号");
                }
                String ems = mEms.getText().toString();
                if (TextUtils.isEmpty(ems)) {
                    T.showShort(mContext, "请输入邮政编码");
                }
                String city = mCity.getText().toString();
                if (TextUtils.isEmpty(city)) {
                    T.showShort(mContext, "请选择城市");
                }
                String detail = mDetail.getText().toString();
                if (TextUtils.isEmpty(detail)) {
                    T.showShort(mContext, "请输入详细地址");
                }

                if (mIsEdit) {
                    EditAddressRequest editAddressRequest = new EditAddressRequest();
                    editAddressRequest.username = name;
                    editAddressRequest.phone = phone;
                    editAddressRequest.zipcode = ems;
                    editAddressRequest.province = provinceS;
                    editAddressRequest.city = cityS;
                    editAddressRequest.district = districtS;
                    editAddressRequest.address = detail;
                    editAddressRequest.addrId = mAddress.getId() + "";
                    mPresenter.editAddress(editAddressRequest);
                } else {
                    AddAddressRequest request = new AddAddressRequest();
                    request.username = name;
                    request.phone = phone;
                    request.zipcode = ems;
                    request.province = provinceS;
                    request.city = cityS;
                    request.district = districtS;
                    request.address = detail;
                    request.is_default = "0";
                    mPresenter.addAddress(request);
                }

                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onCityReadDoneEvent(CityReadDoneEvent event) {
        if (optionsPickerView == null) {
            optionsPickerView = new OptionsPickerView(mContext);
        }
        optionsPickerView.setPicker(CityManeger.provinceNames, CityManeger.cityNames, CityManeger.distirctNames, true);
        optionsPickerView.setTitle("选择地点");
        optionsPickerView.setCyclic(false, false, false);
        optionsPickerView.setSelectOptions(0, 0, 0);
        optionsPickerView.setOnoptionsSelectListener(new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3) {
                String area = "";
                if (CityManeger.distirctList.get(options1).get(option2).size() != 0) {
                    area = CityManeger.provinceList.get(options1).getName()
                            + CityManeger.cityList.get(options1).get(option2).getName()
                            + CityManeger.distirctList.get(options1).get(option2).get(options3).getName();
//                    districtId = "" + CityManeger.distirctList.get(options1).get(option2).get(options3).getId();
                } else {
                    area = CityManeger.provinceList.get(options1).getName()
                            + CityManeger.cityList.get(options1).get(option2).getName();
//                    districtId = "" + CityManeger.cityList.get(options1).get(option2).getId();
                }
                provinceS = CityManeger.provinceList.get(options1).getName();
                cityS = CityManeger.cityList.get(options1).get(option2).getName();
                districtS = CityManeger.distirctList.get(options1).get(option2).get(options3).getName();

                mCity.setText(area);
            }
        });
        optionsPickerView.show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void showLoading() {
        showProgressDialog();
    }

    @Override
    public void hideLoading() {
        hideDialog();
    }

    @Override
    public void onError(String code, String msg) {
        T.showShort(mContext, msg);
    }

    @Override
    public void addAddressSuccess() {
        T.showShort(mContext, "收货地址添加成功!");
        finish();
    }

    @Override
    public void editAddressSuccess() {
        T.showShort(mContext, "收货地址修改成功!");
        finish();
    }
}
